package com.xhtt.modules.inspection.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.modules.inspection.dao.InsTaskUserMapper;
import com.xhtt.modules.inspection.entity.InsTaskDate;
import com.xhtt.modules.inspection.entity.InsTaskUser;
import com.xhtt.modules.inspection.service.IInsTaskDateService;
import com.xhtt.modules.inspection.service.IInsTaskUserService;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xj
 * @since 2019-01-29
 */
@Service
public class InsTaskUserServiceImpl extends ServiceImpl<InsTaskUserMapper, InsTaskUser> implements IInsTaskUserService {

    @Autowired
    private IInsTaskDateService insTaskDateService;

    @Autowired
    private SysUserService sysUserService;

    @Override
    @Transactional
    public void add(List<Integer> users, InsTaskDate insTaskDate) {
        InsTaskUser insTaskUser;
        List<InsTaskUser> insTaskUsers = new ArrayList<>();
        for (Integer user : users) {
            insTaskUser = new InsTaskUser();
            insTaskUser.setUserId(user);
            insTaskUser.setTaskId(insTaskDate.getTaskId());
            insTaskUser.setTaskDateId(insTaskDate.getId());
//            InsTaskUser taskUser = baseMapper
//                    .selectOne(new QueryWrapper<InsTaskUser>()
//                            .eq(InsTaskUser.TASK_DATE_ID, insTaskDate.getId())
//                            .orderByDesc("sort")
//                            .last("limit 1"));
//            // 排序
//            if (null == taskUser) {
//                insTaskUser.setSort(1);
//            } else {
//                insTaskUser.setSort(taskUser.getSort() + 1);
//            }
            insTaskUsers.add(insTaskUser);
        }
        this.saveBatch(insTaskUsers);
    }

    @Override
    public String getUserByTaskIdAndDate(Integer taskId, LocalDateTime dateTime) {

        InsTaskDate insTaskDate = insTaskDateService
                .getOne(new LambdaQueryWrapper<InsTaskDate>()
                        .eq(InsTaskDate::getTaskId, taskId)
                        .eq(InsTaskDate::getDateTime, dateTime));

        if (insTaskDate != null) {
            List<InsTaskUser> insTaskUsers = baseMapper
                    .selectList(new LambdaQueryWrapper<InsTaskUser>()
                            .eq(InsTaskUser::getTaskDateId, insTaskDate.getId()));
            if (CollectionUtils.isNotEmpty(insTaskUsers)) {
                List<Integer> userIds = insTaskUsers.stream().map(InsTaskUser::getUserId).collect(Collectors.toList());
                List<SysUserEntity> sysUserEntities = sysUserService
                        .list(new LambdaQueryWrapper<SysUserEntity>()
                                .in(SysUserEntity::getUserId, userIds));
                return sysUserEntities.stream().map(SysUserEntity::getName).collect(Collectors.joining(","));
            }
        }
        return null;
    }
}
